package org.jtzc.springaiagent.agent.router;

import org.jtzc.springaiagent.agent.BaseAgent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author wu chuang
 * @description
 */
@Service
public class RouterAgent implements BaseAgent {
    private final Map<String, BaseAgent> agentMap;

    @Autowired
    public RouterAgent(
            @Qualifier("faqAgent") BaseAgent faqAgent,
            @Qualifier("techSupportAgent") BaseAgent techSupportAgent,
            @Qualifier("salesAgent") BaseAgent salesAgent) {

        this.agentMap = Map.of(
                "FAQ", faqAgent,
                "TECH", techSupportAgent,
                "SALES", salesAgent
        );
    }

    @Override
    public AgentResponse execute(AgentRequest request) {
        String intent = detectIntent(request.prompt());
        System.out.println("=======================" +intent);
        BaseAgent selectedAgent = agentMap.getOrDefault(intent, agentMap.get("FAQ"));
        return selectedAgent.execute(request);
    }

    private String detectIntent(String prompt) {
        // 简化的意图检测逻辑 - 实际项目中可以使用ML模型
        if (prompt.toLowerCase().contains("how to") || prompt.contains("?")) {
            return "FAQ";
        } else if (prompt.toLowerCase().contains("error") || prompt.contains("not working")) {
            return "TECH";
        } else if (prompt.toLowerCase().contains("buy") || prompt.contains("price")) {
            return "SALES";
        }
        return "FAQ";
    }

}
